package emap;

import java.awt.Image;
import javax.swing.ImageIcon;

import java.awt.Toolkit;
import java.awt.image.*;

/* A Tileset contains an array of tile images available for use.
 **/

class Tileset 
{
	private String myFilename;
	private Image[] myTileImages;
	int numHorizontalTiles = 8, numVerticalTiles;
	
	public Tileset(String filename)
	{
		myFilename = filename;
		//load the picture. 
		
		String path = "tileset/" + myFilename + ".png";
		System.out.println(path);
		ImageIcon ico = new ImageIcon(this.getClass().getResource(path));
		Image bigImg = ico.getImage();
		//get the height of the image.
		numVerticalTiles = (ico.getIconHeight() / 32);
		
		//initialize myTileImages.
		myTileImages = new Image[numVerticalTiles * numHorizontalTiles];
		
		//fill up myTileImages!
		int count = 0;
		for (int y = 0; y < numVerticalTiles; y++)
		{
			for (int x = 0; x < numHorizontalTiles; x++)
			{
				myTileImages[count] = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(bigImg.getSource(), new CropImageFilter(x*32, y*32, 32, 32)));
				count++;
			}
		}
	}
	
	public Image getTileImage(int index)
	{
		if (index < myTileImages.length)
			return myTileImages[index];
		return null;
	}
	public int getNumHorizontalTiles()
	{ return numHorizontalTiles; }
	
	public int getNumVerticalTiles()
	{ return numVerticalTiles; }
}
